﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using HoldemGame;

namespace HoldemView
{
    public partial class HoldemTable : Panel
    {
        private IGameState game;
        private List<PlayerPanel> panels = new List<PlayerPanel>();
        private BoardPanel boardPanel;
        private HoldemTableDirector director;
        //private ActionPanel actionPanel;
        private bool _hasToCommit;

        public const int PlayerPanelWidth = 200;
        public const int PlayerPanelHeight = 200;

        public IGameState Game
        {
            get{return game;}
            set{game=value; GameChanged();}
        }

        public HoldemTableDirector Director
        {
            get { return director;  }
            set
            {
                director = value;
                foreach (Control c in Controls)
                {
                    PlayerPanel p = c as PlayerPanel;
                    if (null != p)
                        p.Director = director;
                }
            }
        }

        public HoldemTable()
        {
            InitializeComponent();


        }

        public void SetMiniView(bool mini)
        {
            foreach (PlayerPanel p in panels)
                p.SetMiniView(mini);
        }

        private void GameChanged()
        {
            UpdateAll();
        }

        public IList<PlayerPanel> PlayerPanels { get { return panels; } }

        

        public int Verify(List<Exception> errors)
        {
            int errs = 0;
            foreach (PlayerPanel pnl in panels)
            {
                errs+=pnl.Verify(errors);
            }
            errs+=boardPanel.Verify(errors);
            return errs;
        }

        public void Commit()
        {
            foreach (PlayerPanel pnl in panels)
            {
                pnl.Commit();
            }
            boardPanel.Commit();
        }

        public void UpdateAll()
        {
            // remove player panels
            foreach (PlayerPanel p in panels)
                this.Controls.Remove(p);
            panels.Clear();

            lblCurrent.Location = new Point(Width / 2, Height / 2);

            if (null == Game)
                return;

            int count = game.Table.Seats.Count;

            int row = (count+1) / 2;

            int i = 0;
            int x = 12;
            int y = 12;

            while (i < row)
            {
                NewPlayerPanel(game.Table.Seats[i], x, y);

                i++;
                x += PlayerPanelWidth;

            }

            y += PlayerPanelHeight*3/2+24;

            while (i < count )
            {

                x -= PlayerPanelWidth;
                NewPlayerPanel(game.Table.Seats[i], x, y);

                i++;
            }
            lblCurrent.Visible = false;

//            UpdateCurrent();

           // SetMiniView(true);
  
            boardPanel = new BoardPanel(Director);
            boardPanel.GameState = game;

            //boardPanel.Size = new System.Drawing.Size(PlayerPanelWidth - 12, PlayerPanelHeight - 12);

            x = 12 + PlayerPanelWidth * (row)/2-boardPanel.Width/2;
            y = 12*3/2 + PlayerPanelHeight*5/4-boardPanel.Height/2;

            boardPanel.Location = new System.Drawing.Point(x, y);
            boardPanel.Name = "board";
            
            boardPanel.TabIndex = panels.Count;
            boardPanel.Visible = true;
            boardPanel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(boardPanel);

            /*
            actionPanel = new ActionPanel(Director);

            y += boardPanel.Height+12;
            x = 12 + PlayerPanelWidth * (1 + leftright / 2) - actionPanel.Width / 2;
            actionPanel.Location = new System.Drawing.Point(x, y);
            actionPanel.Name = "action";
            actionPanel.TabIndex = panels.Count + 1;
            actionPanel.Visible = true;
            actionPanel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(actionPanel);
            */
            UpdateAction(null);

            Refresh();
        }

        public void UpdatePlayers()
        {
            for (int i = 0; i < panels.Count; i++)
            {
                panels[i].Seat = Game.Table.Seats[i];
                //panels[i].PlayerChanged();
            }
            Refresh();
        }

        /*public void UpdateCurrent()
        {
            int leftright = panels.Count / 3;

            for(int i=0;i<panels.Count;i++)
            {
                if (game.Players.Current == game.Players[i])
                {
                    int x = panels[i].Left;
                    int y = panels[i].Top;

                    if (i < leftright)
                        lblCurrent.Location = new Point(x + PlayerPanelWidth, y + (PlayerPanelHeight - lblCurrent.Height) / 2);
                    else if (i >= panels.Count - leftright)
                        lblCurrent.Location = new Point(x - lblCurrent.Width, y + (PlayerPanelHeight - lblCurrent.Height) / 2);
                    else
                        lblCurrent.Location = new Point(x + (PlayerPanelWidth - lblCurrent.Width) / 2, y+PlayerPanelHeight);
                }
            }
            Refresh();
        }*/

        public void UpdateAction(GamePlayer keepPlayer)
        {
            //actionPanel.UpdateAction();
            foreach (PlayerPanel p in this.panels)
            {
              p.UpdateAction(p.Player==keepPlayer);
            }
        }
        public void UpdateBoard()
        {
            boardPanel.UpdateBoard();
        }

        private PlayerPanel NewPlayerPanel(GameSeat seat, int x, int y)
        {
            PlayerPanel pl = new PlayerPanel(director);
            pl.Director = director;
            pl.Seat = seat;
            pl.Location = new System.Drawing.Point(x, y);
            pl.Name = "pl"+panels.Count;
            pl.Size = new System.Drawing.Size(PlayerPanelWidth-12, PlayerPanelHeight-12);
            pl.TabIndex = panels.Count;
            pl.Visible = true;
            pl.BorderStyle = BorderStyle.FixedSingle;
            panels.Add(pl);
            Controls.Add(pl);
            return pl;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
